//
// Created by Key-Box-Min on 21/06/2020.
//

#ifndef COMPILERDOMEA1_CTHREAD_H
#define COMPILERDOMEA1_CTHREAD_H

#include <atomic>
#include <thread>
#include <functional>
#include <future>
#include <list>

#include "AtomicLock.h"
#include "../Base/Server.h"

class CThread : public Lib::Server,public std::thread{
public:
    CThread():std::thread(std::bind(&CThread::run,this)){}
    CThread(std::function<void()> run)
    :std::thread(std::bind(&CThread::work,this))
    {setRun(run);}

    bool isStart(){return status() == Start;}
    void start(bool block = false){setStatus(Start,block);}
    void stop(bool block = false){setStatus(Stop,block);}
    void setRun(std::function<void()> run){std::lock_guard l(__mutex);__run = run;}

protected:
    void work() override {std::lock_guard l(__mutex);__run();}

private:
    std::function<void()> __run;
    std::mutex            __mutex;
};

//----------------------------------



#endif //COMPILERDOMEA1_CTHREAD_H
